package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import com.probox.common.core.utils.SecurityUtils;
import com.probox.customer.api.domain.CustomerFundVo;
import com.probox.common.entity.service.customer.ICustomerFundHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.customer.CustomerFundHistoryMapper;
import com.probox.common.entity.domain.customer.CustomerFundHistory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户资金历史记录Service业务层处理
 *
 * @author usoft
 * @date 2021-07-19
 */
@Service
@Transactional
public class CustomerFundHistoryServiceImpl extends ServiceImpl<CustomerFundHistoryMapper, CustomerFundHistory> implements ICustomerFundHistoryService {
    @Autowired
    private CustomerFundHistoryMapper customerFundHistoryMapper;

    /**
     * 新增客户资金历史记录
     * @param fund
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void saveFundHistory(CustomerFundVo fund) {
        CustomerFundHistory customerFundHistory = new CustomerFundHistory();
        customerFundHistory.setCustomerUid(fund.getCustomerUid());
        customerFundHistory.setPlatformUid(fund.getPlatformUid());
        customerFundHistory.setAllProfit(fund.getAllProfit());
        customerFundHistory.setAllWithdraw(fund.getAllWithdraw());
        customerFundHistory.setAllScore(fund.getAllScore());
        if (fund.getAllFans() != null && fund.getAllFansSecond() != null){
            customerFundHistory.setAllFreeze(Long.parseLong(String.valueOf(fund.getAllFans()+fund.getAllFansSecond())));
        }
        customerFundHistory.setAllBalance(fund.getAllBalance());
        customerFundHistory.setOptTime(DateUtil.date());
        customerFundHistoryMapper.insert(customerFundHistory);
    }


    /**
     查询当前客户资金历史记录
     */
    @Override
    public List<CustomerFundHistory> getFundHistory() {
        QueryWrapper<CustomerFundHistory> historyQueryWrapper = new QueryWrapper<>();
        historyQueryWrapper.eq("CUSTOMER_UID", SecurityUtils.getCustomerUid());
        historyQueryWrapper.eq("PLATFORM_UID",SecurityUtils.getCustomerPlatformUid());
        List<CustomerFundHistory> fundHistories = customerFundHistoryMapper.selectList(historyQueryWrapper);
        return fundHistories;
    }

    /**
     查询当前客户资金历史记录总条数
     */
    @Override
    public Integer getFundHistoryCount() {
        QueryWrapper<CustomerFundHistory> historyQueryWrapper = new QueryWrapper<>();
        historyQueryWrapper.eq("CUSTOMER_UID",SecurityUtils.getCustomerUid());
        historyQueryWrapper.eq("PLATFORM_UID",SecurityUtils.getCustomerPlatformUid());
        List<CustomerFundHistory> fundHistories = customerFundHistoryMapper.selectList(historyQueryWrapper);
        return fundHistories.size();
    }
}
